package com.infovane.action;

import java.util.ArrayList;
import java.util.List;

import com.infovane.base.BaseAction;
import com.infovane.model.TArticleType;
import com.infovane.service.ArticleTypeService;

public class ArticleTypeAction extends BaseAction{
	
	private ArticleTypeService articleTypeService;
	
	
	public ArticleTypeService getArticleTypeService() {
		return articleTypeService;
	}


	public void setArticleTypeService(ArticleTypeService articleTypeService) {
		this.articleTypeService = articleTypeService;
	}


	/**
	 *创建资讯分类
	 * */
	public String create(){
		String typename=httpServletRequest.getParameter("typename");
		String typedescribe=httpServletRequest.getParameter("typedescribe");
		if(typename==null||typename.equals("")){
			String warn="类型名称不能为空!";
			httpServletRequest.setAttribute("WARN", warn);
			return ERROR;
		}else if(typedescribe==null||typedescribe.equals("")){
			String warn="类型描述不能为空!";
			httpServletRequest.setAttribute("WARN", warn);
			return ERROR;
		}else{
			TArticleType tArticleType=new TArticleType();
			tArticleType.setArticleTypeName(typename);
			tArticleType.setArticleTypeDescribe(typedescribe);
			boolean sign=this.articleTypeService.add(tArticleType);
			if(!sign){
				String warn="不可预计错误!";
				httpServletRequest.setAttribute("WARN", warn);
				return ERROR;
			}
		}
		return SUCCESS;
	}
		
	/**
	 *获取资讯所有大类别
	 * */
	public String getArticleType(){
		List<TArticleType> typeList=new ArrayList<TArticleType>();
		typeList=this.articleTypeService.get();
		if(typeList.size()>0){
			httpServletRequest.setAttribute("ArticleType", typeList);
			return SUCCESS;
		}
		return ERROR;
	}
	
	/**
	 *获取资讯大类别对象
	 * */
	public String get(){
		int id=Integer.parseInt(httpServletRequest.getParameter("id"));
		TArticleType articleType=new TArticleType();
		articleType=(TArticleType) this.articleTypeService.get(TArticleType.class,id);
		if(articleType==null){
			String warn="获取数据出错!";
			httpServletRequest.setAttribute("WARN", warn);
			return ERROR;
		}
		httpServletRequest.setAttribute("TARTICLETYPE", articleType);
		return SUCCESS;
	}
	
	/**
	 *删除资讯分类
	 * */
	public String delete(){
		int id=Integer.parseInt(httpServletRequest.getParameter("id"));
		boolean sign=this.articleTypeService.delete(TArticleType.class,id);
		if(!sign){
			String warn="大分类删除出错!";
			httpServletRequest.setAttribute("WARN", warn);
			return ERROR;
		}
		return SUCCESS;
		
	}
	/**
	 * 更新（修改）资讯大类
	 * */
	public String update(){
		int id=Integer.parseInt(httpServletRequest.getParameter("typeid"));
		String typename=httpServletRequest.getParameter("typename");
		String typedescribe=httpServletRequest.getParameter("typedescribe");
		if(typename==null||typename.equals("")){
			String warn="分类名称不能为空!";
			httpServletRequest.setAttribute("WARN", warn);
			return ERROR;
		}else if(typedescribe==null||typedescribe.equals("")){
			String warn="分类描述不能为空!";
			httpServletRequest.setAttribute("WARN", warn);
			return ERROR;
		}else{
			TArticleType articleType=new TArticleType();
			articleType.setArticleTypeId(id);
			articleType.setArticleTypeName(typename);
			articleType.setArticleTypeDescribe(typedescribe);
			boolean sign=this.articleTypeService.update(articleType);
			if(!sign){
				String warn="不可知错误!";
				httpServletRequest.setAttribute("WARN", warn);
				return ERROR;
			}
			return SUCCESS;
		}
	}

}
